package com.heaboy.feedback.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.heaboy.feedback.constant.EnumAttendanceStatus;
import com.heaboy.feedback.domain.*;
import com.heaboy.feedback.service.*;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.heaboy.feedback.mapper.AttendanceMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 签到Service业务层处理
 * 
 * @author heaboy
 * @date 2024-02-01
 */
@Service
public class AttendanceServiceImpl extends ServiceImpl<AttendanceMapper, Attendance> implements IAttendanceService
{
    @Autowired
    private AttendanceMapper attendanceMapper;

    @Autowired
    private ILessonService lessonService;
    @Autowired
    private ICourseService courseService;

    @Autowired
    private ICourseClazzService courseClazzService;
    @Autowired
    private IStudentAttendanceService studentAttendanceService;

    @Autowired
    private ICourseStudentService courseStudentService;
    /**
     * 查询签到
     * 
     * @param id 签到主键
     * @return 签到
     */
    @Override
    public Attendance selectAttendanceById(Long id)
    {
        return attendanceMapper.selectAttendanceById(id);
    }

    /**
     * 查询签到列表
     * 
     * @param attendance 签到
     * @return 签到
     */
    @Override
    public List<Attendance> selectAttendanceList(Attendance attendance)
    {
        return attendanceMapper.selectAttendanceList(attendance);
    }

    /**
     * 新增签到
     * 
     * @param attendance 签到
     * @return 结果
     */
    @Override
    public int insertAttendance(Attendance attendance)
    {
        attendance.setCreateTime(DateUtils.getNowDate());
        return attendanceMapper.insertAttendance(attendance);
    }

    /**
     * 修改签到
     * 
     * @param attendance 签到
     * @return 结果
     */
    @Override
    public int updateAttendance(Attendance attendance)
    {
        attendance.setUpdateTime(DateUtils.getNowDate());
        return attendanceMapper.updateAttendance(attendance);
    }

    /**
     * 批量删除签到
     * 
     * @param ids 需要删除的签到主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceByIds(Long[] ids)
    {
        return attendanceMapper.deleteAttendanceByIds(ids);
    }

    /**
     * 删除签到信息
     * 
     * @param id 签到主键
     * @return 结果
     */
    @Override
    public int deleteAttendanceById(Long id)
    {
        return attendanceMapper.deleteAttendanceById(id);
    }

    @Transactional
    @Override
    public int startSign(Attendance attendance) {

        Attendance attendanceOne = baseMapper.selectOne(new LambdaUpdateWrapper<Attendance>().eq(Attendance::getLessonId, attendance.getLessonId()));
        if(attendanceOne!=null){
            throw new ServiceException("当前课程已经发起过签到 无需重复发起");
        }
        Long lessonId = attendance.getLessonId();
        Lesson lesson = lessonService.getById(lessonId);
        Long courseClazzId = lesson.getCourseClazzId();
        CourseClazz courseClazz = courseClazzService.getById(courseClazzId);
        Course course = courseService.getById(courseClazz.getCourseId());
        attendance.setCourseId(course.getId());
        attendance.setExpectedAttendees(course.getCourseStudentsCount());
        attendance.setTeacherId(SecurityUtils.getUserId());
        attendance.setStartTime(DateUtils.getNowDate());
        attendance.setCreateTime(DateUtils.getNowDate());
        attendance.setCreateBy(SecurityUtils.getUsername());
        attendance.setEndTime(DateUtils.addMinutes(attendance.getStartTime(),5));
       boolean flag = save(attendance);
        //插入学生签到状态
        List<CourseStudent> courseStudents = courseStudentService.selectCourseStudentList(new CourseStudent().setCourseId(course.getId()));
        List<StudentAttendance> studentAttendances = new ArrayList<>(courseStudents.size());
        for (CourseStudent courseStudent : courseStudents) {
            StudentAttendance studentAttendance = new StudentAttendance()
                    .setAttendanceId(attendance.getId())
                    .setStudentId(courseStudent.getStudentId())
                    .setLessonId(attendance.getLessonId());
            studentAttendance.setCreateBy(SecurityUtils.getUsername());
            studentAttendance.setCreateTime(DateUtils.getNowDate());
            studentAttendance.setStatusCode(EnumAttendanceStatus.START.getCode());
            studentAttendances.add(studentAttendance);
        }

        flag=flag&studentAttendanceService.saveBatch(studentAttendances);
        return flag?1:0;
    }
}
